Ext.define('Zm.controller.DevDef', {
    extend: 'Ext.app.Controller',
    uses:['Zm.Common','Zm.i18n.Lang'],
	stores:['DevDef','TypeDef','Catalog'],
    views:['model.Edit','model.CatEdit'],
    refs:[
        {ref:'List', selector:'modellist'},
        {ref:'Catalog', selector:'modelcatalog'}
    ],
    init: function(){
        var me = this;
        me.control({
			'modellist button[action=add]':{
				click:me.onAdd
			},
            'modellist button[action=edit]':{
                click:me.onEdit
            },
            'modellist button[action=del]':{
                click:me.onDel
            },
            'modellist':{
                itemdblclick: me.onEdit
            },
            'modeledit button[action=save]':{
                click:me.saveDef
            },
            'modelcatedit button[action=save]':{
                click:me.saveCat
            },
            'modelcatalog':{
                recorddrop:me.onDropRecord,
                change:me.onCatChange,
                catadd:me.onAddCat,
                catdel:me.onDelCat,
                catedit:me.onEditCat,
                catrefresh:me.onRefCat   
            }
        });
    },
	onAdd:function(){
		var view = Ext.widget('modeledit');
	},
    onDel:function(btn){
        var me = this,ids='',i,
            lang = Zm.i18n.Lang,
            sm = me.getList().getSelectionModel(),
            records = sm.getSelection();
        if(records.length>0){
            for(i=0;i<records.length;i++){
                if(ids!='') ids += ',';
                ids += records[i].get('ddid');
            }
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg: Ext.String.format(lang.tDelModelTip,records.length),
                   buttons: Ext.Msg.YESNO,
                   fn: me.onDelCallback,
                   scope: me,
                   ids:ids,
                   icon: Ext.Msg.QUESTION
               });                        
        }
    },
    onDelCallback: function(btn,text,opt){
        if(btn == 'yes'){
            var me = this,
                lang = Zm.i18n.Lang,
                cm = Zm.Common;
            Ext.Ajax.request({
                url: g_app + '/DevDef/del',
                params: {ids: opt.ids},
                scope: me,
                success:function(response,options){ 
                     var obj = Ext.decode(response.responseText,true);
                     if(obj==null) return;
                     if(obj.success){
                         me.getDevDefStore().load();                            
                         cm.infoMsg(lang.tSucc,obj.msg);
                     }else{
                         cm.errMsg(lang.tFail, obj.msg); 
                     }
                },
                failure:cm.onAFail
             });    
        }
    },
    onEdit:function(){
        var me = this,
            sm = me.getList().getSelectionModel(),
            records = sm.getSelection(),
            itemList,
            view;
        if(records.length>0){
            view = Ext.widget('modeledit');   
            view.down('form').loadRecord(records[0]);
            itemList = view.down('#attrlist');
            if(itemList) itemList.setValue(records[0].get('ddid'));
        }
    },
    saveDef:function(btn){
        var me = this,attrval='',
            win = btn.up('window'),
            ddid = win.down('#ddid').getValue(),
            ddname = win.down('#ddname').getValue(),
            modelurl = win.down('#modelurl').getValue(),
            memo = win.down('#memo').getValue(),            
            attrlist = win.down('#attrlist');
        btn.setDisabled(true);//禁止重复保存
        if(attrlist) {
            attrval=attrlist.getValue();
        }
        Ext.Ajax.request({
            url: g_app + '/DevDef/save',
            scope: me,
            params:{
                attrs:Ext.encode(attrval),
                ddid:ddid,
                ddname:ddname,
                modelurl:modelurl,
                memo:memo,
                cid:me.getList().groupId
            },
            success: function(form,action){
                me.getDevDefStore().load();
                win.close();
            },
            failure: Zm.Common.onAFail
        });     
    },
    onDropRecord:function(view,ids,cid,isAdd){
        var me = this,
            lst = me.getList();
        Ext.Ajax.request({
            url: g_app + '/DevDef/dropCat',
            params: {ids: ids,cid:cid,sid:lst.groupId,add:isAdd?1:0},
            scope: me,
            success:function(response,options){ 
                 var obj = Ext.decode(response.responseText,true);
                 if(obj==null) return;
                 if(obj.success){
                     me.getDevDefStore().load();
                 }
            },
            failure:Zm.Common.onAFail
         });                
    },
    onAddCat:function(view,node){
        var view = Ext.widget('modelcatedit');
        view.down('form').getForm().setValues({parentid:node.get('cid')});
    },
    saveCat:function(btn){
        var me = this,
            win = btn.up('window'),
            frm = win.down('form'),
            form = frm.getForm();
        btn.setDisabled(true);//禁止重复保存
        form.submit({
            url: g_app + '/DevDef/saveCat',
            scope: me,
            success: function(form,action){
                var data = action.result.data,
                    store = me.getCatalog().store,
                    node,rd;
                if(data.add){
                    rd = Ext.create('Zm.model.Catalog');
                    rd.set(data);
                    rd.commit();
                    node = store.getNodeById(data.parentid);
                    if(node){
                        node.appendChild(rd);
                    }
                }else{
                    node = store.getNodeById(data.cid);
                    if(node) node.set(data);
                }
                win.close();
            },
            failure: Zm.Common.onFail
        });
    },
    onCatChange:function(view,record){
        var me = this;
        me.getList().setCatalog(record.get('cid'));
    },
    onDelCat:function(view,node){
        var me = this;
        Ext.Ajax.request({
            url: g_app + '/DevDef/delCat',
            params: {cid:node.get('cid')},
            scope: me,
            success:function(response,options){ 
                 var obj = Ext.decode(response.responseText,true),
                    store = me.getCatalog().store,
                    node;
                 if(obj==null) return;
                 if(obj.success){
                    node = store.getNodeById(obj.data.cid);
                    if(node) node.remove(true);
                 }
            },
            failure:Zm.Common.onAFail
         });                
    },
    onEditCat:function(view,node){
        var view = Ext.widget('modelcatedit');
        view.down('form').loadRecord(node);
    },
    onRefCat:function(){
        this.getCatalogStore().load();    
    }
});